home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / amigae.june.archive / 000041_crash!kirk.safb.af.mil!BWILLS_Wed, 9 Jun 93 20:30:18 PST.msg < prev    next >
Text File  |  1993-08-31  |  3KB  |  121 lines

  1. Received: by bkhouse.cts.com (V1.16/Amiga)
  2.     id AA00000; Wed, 9 Jun 93 20:30:18 PST
  3. Received: from kirk.safb.af.mil by crash.cts.com with smtp
  4.     (Smail3.1.28.1 #15) id m0o3avw-0000C8C; Wed, 9 Jun 93 17:56 PDT
  5. Message-Id: <m0o3avw-0000C8C@crash.cts.com>
  6. Date: 9 Jun 93 19:53:00 CST
  7. From: "Barry D. Wills" <BWILLS@kirk.safb.af.mil>
  8. To: "amigae" <amigae@bkhouse.cts.com>
  9. Subject: Linked Lists and Lists (3 of 3)
  10.  
  11. ENUM ER_NONE,
  12.      ER_MEM,
  13.      ER_USER_ABORT
  14.  
  15. RAISE ER_MEM IF List () = NIL,
  16.       ER_MEM IF String () = NIL,
  17.       ER_USER_ABORT IF CtrlC () = TRUE
  18.  
  19. OBJECT listElementType
  20.   name, phoneNumber
  21. ENDOBJECT
  22.  
  23. PROC newElementFor (name, phoneNumber)
  24.   DEF el : PTR TO listElementType
  25.   el := New (SIZEOF listElementType)
  26.   el.name := String (10)
  27.   StrCopy (el.name, name, ALL)
  28.   el.phoneNumber := phoneNumber
  29. ENDPROC  el
  30.  
  31. PROC phoneNumberFrom (theLink)
  32.   DEF link : PTR TO listElementType
  33.   link := ^theLink
  34. ENDPROC  link.phoneNumber
  35.  
  36. PROC printAll (theList)
  37.   DEF link,
  38.       element : PTR TO listElementType
  39.   link := theList
  40.   WHILE link <> NIL
  41.     element := ^link
  42.     WriteF ('\s \d\n', element.name, element.phoneNumber)
  43.     link := Next (link)
  44.   ENDWHILE
  45. ENDPROC
  46.  
  47. PROC tailOfList (theList)
  48.   DEF tail
  49.   tail := theList
  50.   WHILE Next (tail) <> NIL DO tail := Next (tail)
  51. ENDPROC  tail
  52.  
  53. PROC appendList (theList, name, phoneNumber)
  54.   DEF newElement, newList
  55.  
  56.   newList := List (1)
  57.   newElement := newElementFor (name, phoneNumber)
  58.   ListCopy (newList, [newElement], ALL)
  59.  
  60.   IF ^theList = NIL  /* Empty list, special case. */
  61.     ^theList := newList
  62.   ELSE
  63.     VOID Link (tailOfList (^theList), newList)
  64.   ENDIF
  65. ENDPROC
  66.  
  67. PROC sortList (theList)
  68.   DEF listCurrent : PTR TO listElementType,
  69.       listNext : PTR TO listElementType,
  70.       temp
  71.  
  72.   listCurrent := ^theList
  73.   WHILE listCurrent <> NIL
  74.     listNext := listCurrent
  75.     WHILE listNext := Next (listNext)
  76.       CtrlC ()
  77.       IF phoneNumberFrom (listCurrent) > phoneNumberFrom (listNext)
  78.         temp := ^listCurrent
  79.         ^listCurrent := ^listNext
  80.         ^listNext := temp
  81.       ENDIF
  82.     ENDWHILE
  83.     listCurrent := Next (listCurrent)
  84.   ENDWHILE
  85. ENDPROC
  86.   /* sortList */
  87.  
  88. PROC main () HANDLE
  89.   DEF listHead = NIL
  90.  
  91.   /* Allocate and initialize front of list. */
  92.   appendList ({listHead}, 'Cookamonga', 4444444)
  93.   printAll (listHead)
  94.  
  95.   /* Allocate and initialize a second list element, then link it. */
  96.   appendList ({listHead}, 'Beeblebrox', 3333333)
  97.   printAll (listHead)
  98.  
  99.   /* Allocate and initialize a third list element, then link it. */
  100.   appendList ({listHead}, 'Anteater', 2222222)
  101.   printAll (listHead)
  102.  
  103.   /* Allocate and initialize a fourth list element, then link it. */
  104.   appendList ({listHead}, 'Aardvark', 1111111)
  105.   printAll (listHead)
  106.  
  107.   /* Sort the list and display the contents. */
  108.   sortList ({listHead})
  109.   printAll (listHead)
  110.  
  111.   DisposeLink (listHead)  /* WARNING!  listHead will be a NIL pointer    */
  112.                           /* after this function call even though it was */
  113.                           /* not allocated using the List () function.   */
  114.  
  115.   WriteF ('\n\n')
  116.   CleanUp (0)
  117.  
  118. EXCEPT
  119.   IF exception = ER_MEM THEN WriteF ('\n\n *** Out of memory.\n\n')
  120.   CleanUp (exception)
  121. ENDPROC